<?php

namespace Swoft\Admin\Form\Field;

use Swoft\Admin\Admin;

class MultipleSelect extends Select
{
    /**
     * Other key for many-to-many relation.
     *
     * @var string
     */
    protected $otherKey;


    public function fill($data)
    {
        $relations = array_get($data, $this->column);

        if (is_string($relations)) {
            $this->value = explode(',', $relations);
        }

        if (is_array($relations)) {
            if (is_null(current($relations))) {
                $this->value = null;
            } else {
                $this->value = $relations;
            }
        }
    }

    public function setOriginal(array $data)
    {
        $relations = array_get($data, $this->column);

        if (is_string($relations)) {
            $this->original = explode(',', $relations);
        }

        if (is_array($relations)) {
            if (is_null(current($relations))) {
                $this->original = null;
            } else {
                $this->original = $relations;
            }
        }
    }

    public function prepare($value)
    {
        return implode(',', (array) $value);
    }

    public function render()
    {
        Admin::script('setTimeout(function () {$("input.select2-search__field").css("width", "300px")},100)');
        return parent::render(); // TODO: Change the autogenerated stub
    }
}
